package com.yikaiye.android.yikaiye.widget;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.yikaiye.android.yikaiye.R;

/**
 * 自定义下拉刷新Header
 */
public class MyClassicsHeader extends LinearLayout implements RefreshHeader {

    private ImageView iv;
    private TextView text;

    // 方式一
    private AnimationDrawable mProgressDrawable;

    public MyClassicsHeader(Context context) {
        super(context);
        initView(context);
    }

    public MyClassicsHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initView(context);
    }

    public MyClassicsHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    private void initView(Context context) {
        View view = View.inflate(context, R.layout.hearder, this);
        iv = view.findViewById(R.id.logo);
        text = view.findViewById(R.id.text);
    }

    @NonNull
    public View getView() {
        // 真实的视图就是自己，不能返回null
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int headHeight, int maxDragHeight) {
        // 方式一
//        mProgressDrawable = new AnimationDrawable();
//        //准备好资源图片
//        int[] ids = {R.mipmap.ani1, R.mipmap.ani2, R.mipmap.ani3, R.mipmap.ani4, R.mipmap.ani5, R.mipmap.ani6, R.mipmap.ani7, R.mipmap.ani8};
//        //通过for循环添加每一帧动画
//        for (int id : ids) {
//            @SuppressLint("UseCompatLoadingForDrawables")
//            Drawable frame = getResources().getDrawable(id);
//            //设定时长
//            mProgressDrawable.addFrame(frame, 100);
//        }
//        iv.setImageDrawable(mProgressDrawable);
//        mProgressDrawable.start();

//        // 方式二，旋转动画效果
//        ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0, 360);
//        rotation.setDuration(2000);
//        rotation.start();

        // 方式三，上下跳动效果
        ValueAnimator animator = ValueAnimator.ofInt(0, -40, 0);
        animator.addUpdateListener(animation -> {
            int currentValue = (Integer) animation.getAnimatedValue();
            iv.setTranslationY(currentValue);
            iv.requestLayout();
        });
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setDuration(1000);
        animator.start();
    }

    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {
        // 方式一
        if (mProgressDrawable != null) {
            mProgressDrawable.stop();
        }
        // 延迟500毫秒之后再弹回
        return 500;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
                text.setText("下拉开始刷新");
                break;
            case RefreshReleased:
                text.setText("正在刷新...");
                break;
            case ReleaseToRefresh:
                text.setText("释放立即刷新");
                break;
            case RefreshFinish:
                text.setText("刷新完成");
                break;
        }
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
    }

    @Override
    public void setPrimaryColors(@ColorInt int... colors) {
    }
}